package com.java.zhuiyun.api.image_file.service.impl;

import com.java.zhuiyun.api.image_file.dao.ImageFileMapper;
import com.java.zhuiyun.api.image_file.entity.ImageFile;
import com.java.zhuiyun.api.image_file.service.ImageFileService;
import com.java.zhuiyun.common.errorCode.CommonException;
import com.java.zhuiyun.common.errorCode.EmErrorCode;
import com.java.zhuiyun.config.VariableConfig;
import com.java.zhuiyun.util.GetUuid;
import com.java.zhuiyun.util.StringUtils;
import com.java.zhuiyun.util.UploadFileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.common.Mapper;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;
import java.util.List;

/**
 * @ClassName ImageFileServiceImpl
 * @Description TODO
 * @Author MJL
 * @Date 2023/9/13 22:50
 **/
@Service
@Slf4j
public class ImageFileServiceImpl implements ImageFileService {
    @Resource
    VariableConfig variableConfig;
    @Resource
    ImageFileMapper imageFileMapper;
    @Resource
    Mapper<ImageFile> mapper;
    @Override
    public String addImageFile(MultipartFile file) {
        // 判断文件是否为空
//判断文件是否为空        if (file == null) {
//            return "文件类型为空";
//        }
        // 获取图片大小
        long fileSize = file.getSize();
        // 获取图片类型
        String fileType = file.getContentType();
        // 上传图片至腾讯云
        String uploadFile = UploadFileUtils.uploadFile(file);
        ImageFile imgFile = new ImageFile();
        String uuid = GetUuid.getUuid();
        // 获取图片名称
        String fileUuid = uuid + "." + fileType.replace("image/","");
        // 添加图片大小
        imgFile.setFileSize(String.valueOf(fileSize));
        // 添加图片名称
        imgFile.setFileUuidName(fileUuid);
        // 添加图片类型
        imgFile.setFileType(fileType.replace("image/",""));
        // 添加图片服务器id
        imgFile.setImageName(Integer.valueOf(uploadFile));
        // 添加时间
        imgFile.setCreateTime(new Date());
        // 修改时间
        imgFile.setUpdateTime(new Date());
        // 删除标识符
        imgFile.setDelFlag(0);
        imageFileMapper.addImageFile(imgFile);

        Integer id = imgFile.getId();
        System.out.println("数据 " + id);
        return uploadFile;

    }

    @Override
    public void getImageById(Integer id, HttpServletResponse response) throws CommonException {
        if(StringUtils.isNotEmpty(id)){
            // 获取文件对象
            ImageFile imageFile = mapper.selectByPrimaryKey(id);
            if(imageFile!=null&&variableConfig.getImageType().contains(imageFile.getFileType())){
                // 拼接图片路径
                File file = new File(variableConfig.getRootPath() + File.separator + imageFile.getFilePath() + File.separator + imageFile.getFileUuidName());
                if(file.exists()){
                    FileInputStream fis = null;
                    OutputStream os = null;
                    try {
                        // 将图片读取到文件输入流中
                        fis = new FileInputStream(file);
                        byte[] data = new byte[(int) file.length()];
                        fis.read(data);
                        response.setContentType("image/png;charset=utf-8");
                        // 获取输出流对象
                        os = response.getOutputStream();
                        // 写入
                        os.write(data);
                        os.flush();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }finally {
                        // 关闭流
                        closeIO(os,fis);
                    }
                }else{

                }
            }else{

            }
        }else {
            throw new CommonException(EmErrorCode.ID_NULL);
        }
    }

    @Override
    public List<ImageFile> selectAll() {
        List<ImageFile> list=imageFileMapper.selectAllImage();
        return list;
    }

    public void closeIO(OutputStream os,FileInputStream fis){
        // 关闭文件输入流
        if(fis!=null){
            try {
                fis.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        // 关闭输出流
        if(os!=null){
            try {
                os.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
